<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-if="false" v-model="listQuery.lineId" :placeholder="$t('equipment.lineNumber')" class="filter-item" style="width: 200px" @change="handleLineSelect">
        <el-option v-for="item in lineOptions" :key="item.lineId" :label="item.lineName" :value="item.lineId" />
      </el-select>
      <el-select v-model="listQuery.typeNo" :placeholder="$t('produce.typeName')" class="filter-item" style="width: 200px" clearable>
        <el-option v-for="type in typeOptions" :key="type.typeNo" :label="type.typeName" :value="type.typeNo" />
      </el-select>
      <el-select v-model="listQuery.processNumber" :placeholder="$t('equipment.processNumber')" class="filter-item" clearable style="width: 350px" @change="handleFilter">
        <el-option v-for="process in processOptions" :key="process.processNumber" :label="process.processName+'_'+process.processNumber" :value="process.processNumber" />
      </el-select>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-refresh-left" @click="handleFilter">
        {{ $t('common.load') }}
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-folder-add" @click="handleSave">
        {{ $t('common.save') }}
      </el-button>
    </div>
    <!-- 主界面数据 -->
    <el-table
      ref="dragTable"
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      :header-row-style="{color: 'black'}"
      :header-cell-style="{background: 'lightBlue'}"
      border
      fit
      row-key="id"
      highlight-current-row
      :row-style="{height:'20px'}"
      :cell-style="{padding:'2px'}"
      style="width: 100%;"
    >
      <el-table-column :label="$t('equipment.resultCode')" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.processResultDef.resultCode }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.resultName')" width="250px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.processResultDef.resultName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.processName')" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.processResultDef.processName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.resultUnit')" width="150px" align="center">
        <template slot-scope="{row}">
          <el-input v-model="row.unit" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('tracibility.minValue')" width="150px" align="center">
        <template slot-scope="{row}">
          <el-input v-model="row.minValue" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('tracibility.standardValue')" width="150px" align="center">
        <template slot-scope="{row}">
          <el-input v-model="row.standardValue" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('tracibility.maxValue')" width="150px" align="center">
        <template slot-scope="{row}">
          <el-input v-model="row.maxValue" />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getLineList, getProcessList } from '@/api/equipment'
import { getTypeList, getTypeProcessSetting, saveTypeProcessSetting } from '@/api/produce'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import i18n from '@/lang'

export default {
  name: 'LineDefinition',
  components: {
  },
  directives: { waves },
  data() {
    return {
      editRight: true,
      tableKey: 0,
      list: [],
      lineOptions: [],
      typeOptions: [],
      processOptions: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        state: 0,
        name: '',
        lineId: this.$store.getters.lineId,
        lineNumber: this.$store.getters.lineNumber,
        lineName: '',
        typeNo: '',
        stationNumber: '',
        processNumber: '',
        description: '',
        startTime: parseTime(Date.now(), '{y}-{m}-{d} 00:00:00'),
        endTime: '',
        sort: '+due_date'
      },
      showReviewer: false,
      temp: {
        id: 0,
        projectNo: '',
        description: '',
        priority: '',
        manager: '',
        dueDate: '',
        startDate: '',
        state: 0,
        remark: '',
        companyCode: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      dialogPvVisible: false,
      pvData: [],
      rules: {
        lineName: [{ required: true, message: i18n.t('msg.CAN_NOT_BE_NULL'), trigger: 'change' }],
        lineNo: [{ required: true, message: i18n.t('msg.CAN_NOT_BE_NULL'), trigger: 'change' }],
        priority: [{ required: true, message: i18n.t('msg.CAN_NOT_BE_NULL'), trigger: 'change' }]
      },
      downloadLoading: false,
      searchLoading: {
        company: false,
        manager: false
      },
      searchDialogStatus: '',
      searchDialogFormVisible: {
        company: false,
        manager: false
      }
    }
  },
  created() {
    this.handleLineSelect()
    this.getProcessOptions()
    this.editRight = (this.$store.getters.roles === 'admin' || this.$store.getters.roles === 'administrator')
  },
  methods: {
    formatDateTime(date) {
      return date.substring(0, 10)
    },
    // 判断线体ID是否为空
    isEmptyLineId: function() {
      var ret = (this.listQuery.lineId === undefined || this.listQuery.lineId === 0)
      if (ret) {
        this.$notify({
          title: this.$i18n.t('common.tip'),
          message: i18n.t('msg.NO_LINE_SELECTED'),
          type: 'warn',
          duration: 2000
        })
      }
      return ret
    },
    // 判断型号是否为空
    isEmptyTypeNo: function() {
      var ret = (this.listQuery.typeNo === undefined || this.listQuery.typeNo === '')
      if (ret) {
        this.$notify({
          title: this.$i18n.t('common.warn'),
          message: i18n.t('msg.NO_TYPENO_SELECTED'),
          type: 'warn',
          duration: 2000
        })
      }
      return ret
    },
    // 获取线体清单
    getLineList() {
      this.listLoading = true
      getLineList(this.listQuery).then(response => {
        this.lineOptions = response.data.lines
        if (this.lineOptions.length > 0) {
          this.listQuery.lineId = this.lineOptions.lineId
        }
        this.processOptions = []
        this.typeOptions = []
        this.listLoading = false
      })
    },
    // 获取工艺流程选项
    getProcessOptions() {
      this.listLoading = true
      this.processOptions = []
      getProcessList(this.listQuery).then(response => {
        this.processOptions = response.data.processes
        this.listLoading = false
      })
    },
    handleLineSelect() {
      this.typeOptions = []
      this.stationOptions = []
      getTypeList(this.listQuery).then(response => {
        this.typeOptions = response.data.types
        if (this.typeOptions.length > 0) this.listQuery.typeNo = this.typeOptions[0].typeNo
      })
      this.listQuery.processNumber = ''
      this.processOptions = []
      this.List = []
      getProcessList(this.listQuery).then(response => {
        this.processOptions = response.data.processes
      })
    },
    // 获取清单
    getList() {
      if (this.isEmptyLineId() === false && this.isEmptyTypeNo() === false) {
        this.listLoading = true
        this.list = []
        getTypeProcessSetting(this.listQuery).then(response => {
          response.data.processResults.forEach(e => {
            var index = 0
            var update = false
            var typeSetting
            if (response.data.typeSettings !== null) {
              for (var i = 0; i < response.data.typeSettings.length; i++) {
                if (response.data.typeSettings[i].resultCode === e.resultCode) {
                  typeSetting = response.data.typeSettings[i]
                  index = i
                  update = true
                  break
                }
              }
            }
            if (update) {
              this.list.push({
                checked: true,
                processResultDef: e,
                minValue: typeSetting.minValue,
                maxValue: typeSetting.maxValue,
                standardValue: typeSetting.standardValue,
                resultCode: e.resultCode,
                processResultId: typeSetting.processResultId,
                typeNo: typeSetting.typeNo,
                unit: typeSetting.unit })
              response.data.typeSettings.splice(index, 1)
            } else {
              this.list.push({
                checked: true,
                processResultDef: e,
                minValue: '',
                maxValue: '',
                standardValue: '',
                resultCode: e.resultCode,
                processResultId: e.processResultId,
                typeNo: this.listQuery.typeNo,
                unit: e.resultUnit })
            }
          })
          this.total = response.data.total
          this.listLoading = false
        })
      }
    },
    // 点击搜索时获取清单
    handleFilter() {
      this.getList()
    },
    // 点击搜索时获取清单
    handleSave() {
      if (this.isEmptyLineId() === false && this.isEmptyTypeNo() === false) {
        saveTypeProcessSetting(this.list).then(response => {
          this.$notify({
            itle: this.$i18n.t('common.tip'),
            message: i18n.t('msg.UPDATE_SUCCESS'),
            type: 'success',
            duration: 2000
          })
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  height: 800px;
  background-color: white;
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px x0;
    //margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
